<?php

/*
 * @file Order.php
 * @description Model de données menu
 * @author Alexis Thou
 * @date 4 sept. 2012
 * 
 */

class Model_Order extends Model {

    private $_table = "order";
    private $_primary_key = "id";
    private static $_instance;
    
    static function getInstance() {
        if (empty(self::$_instance))
            self::$_instance = new Model_Order();
        return self::$_instance;
    }
    function __construct() {
        parent::__construct($this->_table);
    }

    function getMenuByTid($tid) {

        $result = $this->_db->query('SELECT * from menu as m 
                           LEFT JOIN `order` as o on  o.menu_id = m.id 
                           WHERE m.id IN(SELECT menu_id as id FROM `order` WHERE table_id = ' . $tid . ') AND table_id='.$tid.'');
        return $result->fetchAll(PDO::FETCH_OBJ);
    }

    
    

    /**
     * Vérifie si un menu est deja present à une table
     * @param type $tid table_id
     * @param type $mid menu_id
     * @return type 
     */
    function check_existed_menu($tid, $mid) {
        $result = $this->select('count(*) as count', array('table_id' => $tid, 'menu_id' => $mid));
        return $result->count > 0 ? true : false;
    }

    function update_amount($tid, $mid,$amount = 0) {
        $sql = 'UPDATE `order` SET `amount` = `amount`+1 WHERE menu_id = '.$mid.' AND table_id = '.$tid;
        $stmt =  $this->_db->prepare($sql);
        $result =  $stmt->execute();
        return $result;
    }

}

?>
